import lPainter from '@/components/lime-painter/';
import { mapState } from 'vuex';

export default {
  components: {
    lPainter,
  },
  props: {
    srcImage: {
      type: String,
      default: '/static/images/poster_bg.png',
    },
  },
  data() {
    return {
      poster: '',
    };
  },
  computed: {
    ...mapState({
      user: (state) => state.user,
    }),
    link() {
      const { userCode } = this.user;
      //console.log(this.user.promotionLink);
      //console.log(process.env.VUE_APP_PATH);
      if (process.env.NODE_ENV === 'development') {
        return (
          window.location.origin +
          process.env.VUE_APP_PATH +
          '/register?id=' +
          userCode
        );
      } else {
        return this.user.promotionLink + '/register?id=' + userCode;
      }
    },
    base() {
      const { userCode } = this.user;
      return {
        width: '620rpx',
        height: '930rpx',
        views: [
          {
            type: 'image',
            src: this.srcImage,
            css: {
              top: '0rpx',
              left: '0rpx',
              width: '620rpx',
              height: '930rpx',
            },
          },
          {
            type: 'qrcode',
            text: this.link,
            css: {
              top: '630rpx',
              left: '238rpx',
              width: '150rpx',
              height: '150rpx',
            },
          },

          {
            type: 'text',
            text: `邀请码：${userCode}`,
            css: {
              top: '820rpx',
              left: '228rpx',
              fontSize: '24rpx',
              color: '#ffffff',
            },
          },
        ],
      };
    },
  },
  methods: {
    onSuccess(value) {
      this.poster = value;
    },
    toCopy() {
      uni.setClipboardData({
        data: this.user.promotionLink + '/invite-poster',
        success: function () {
          uni.showToast({
            title: '复制成功',
            icon: 'none',
          });
        },
      });
    },
    onSave() {
      if (!this.poster) return;
      // #ifdef APP-PLUS
      uni.saveImageToPhotosAlbum({
        filePath: this.poster,
        success() {
          uni.showToast({
            title: '保存成功',
          });
        },
      });
      // #endif

      // #ifdef H5
      const params = this.poster.split(',');
      const bytes = atob(params[1]);
      const ab = new ArrayBuffer(bytes.length);
      const ua = new Uint8Array(ab);
      for (let i = 0; i < bytes.length; i++) {
        ua[i] = bytes.charCodeAt(i);
      }
      var blob = new Blob([ab], { type: 'application/octet-stream' });
      var url = URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.href = url;
      a.download = new Date().valueOf() + '.png';
      document.body.appendChild(a);
      a.click();
      a.remove();
      URL.revokeObjectURL(url);
      // #endif
    },
    onCopy() {
      uni.setClipboardData({
        data: this.link,
        success: () => {
          uni.showToast({
            title: '复制成功',
            icon: 'none',
          });
        },
      });
    },
  },
};
